package cn.com.roy.thread.t5_1_2.timertest2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Run4 {

    static int i = 0;


    static public class MyTaskA extends TimerTask {
        @Override
        public void run() {
            System.out.println("正常执行了：" + i);
        }
    }

    //Timer类中的cancle方法有时候并没有争抢到queue锁，所以TimnerTask的任务继续正常执行
    public static void main(String[] args) {
       while(true){
           try {
               i++;
               MyTaskA taskA = new MyTaskA();
               Timer timer = new Timer();
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
               String dateString = "2018-04-16 20:23:10";
               Date dateRef = sdf.parse(dateString);
             //  System.out.println("字符串时间为：" + dateRef.toLocaleString() + "当前时间为：" + new Date().toLocaleString());
               timer.schedule(taskA, dateRef);
               timer.cancel();
           } catch (ParseException e) {
               e.printStackTrace();
           }
       }
    }

}
